<?php
	/*==================================================================*/
	/*		文件名:Image.class.php                              */
	/*		概要: 图片管理类.                	       	    */
	/*		作者: 高洛峰                                        */
	/*		创建时间: 2009-05-25                                */
	/*		最后修改时间:2009-05-25                             */
	/*		copyright (c)2009 lampteacher@gmail.com             */
	/*==================================================================*/
	class ImageModel extends  Model{	
		function __construct(){
			parent:: __construct();
			$this->tabName = TAB_PREFIX."picture";
 			$this->fieldList=array("id","picTitle", "description", "picName", "pid", "hasThumb", "hasMark");
		}


		function validateForm($post){
		        $result=true;
		
			if(!Validate::required($post['picTitle'])) {
				$this->messList[] = "图片标题不能为空.";
				$result=false;
			}
			if(!Validate::checkLength($post['description'], 200)) {
				$this->messList[] = "图片描述不能超过200个字符.";
				$result=false;
			}
			return  $result;
		}

		private function upload(){
			$fileUp=new FileUpload(array("filePath"=>GALLERY_REAL_PATH));
			if($fileUp->uploadFile($_FILES["pic"])<0){
				return array(0, $fileUp->getErrorMsg());
			}else{
				return array(1, $fileUp->getNewFileName());
			}	
		}

		function addPic($post){
			if($this->validateForm($post)){
				$image=new Image(GALLERY_REAL_PATH);
				
				//上传图片
				$up=$this->upload();
				if($up[0]){
					global  $pictureSize;
					$imageName=$image->thumb($up[1], $pictureSize["maxWidth"], $pictureSize["maxWidth"],"");
					if($imageName){
						$post["picName"]=$imageName;
						$this->messList[]="图片上传成功";
					}else{
						$this->messList[] = "改变图片大小失败！";
						return false;
					}
				}else{
					$this->messList[]=$up[1];
					return;
				}

				if(!empty($post['hasThumb'])){ //生成缩略图
					global $thumbSize;	
					if($image->thumb($imageName,$thumbSize["width"], $thumbSize["height"])){
						$this->messList[] = "修改缩略图片成功！";
					}else{
						$this->messList[] = "修改缩略图片失败！";
						return false;
					}
				}else{
					$post['hasThumb']=0;
				}
				if(!empty($post['hasMark'])){	//加水印
					global $waterText;
					if($image->waterMark($imageName, WATER, POSITION)){
						$this->messList[] = "添加水印成功！";
					}else{
						$this->messList[] = "添加水印失败！";
						return false;
					}
				}else{
					$post['hasMark']=0;
				}

				//向数据库中添加图片
				if($this->add($post)){
					$this->messList[] = "图片添加成功！";
					return true;
				}else{
					$this->messList[] = "图片添加失败！";
					return false;
				}
			}else{
				$this->messList[] = "图片添加失败！";
				return false;
			}
		}
		//删除修改前的图片
		private function delsrcimg($imageName){
			$image=GALLERY_REAL_PATH.$imageName;
			$image_th=GALLERY_REAL_PATH."th_".$imageName;
			$image_wa=GALLERY_REAL_PATH."wa_".$imageName;

			if(file_exists($image)){
				unlink($image);
			}

			if(file_exists($image_th)){
				unlink($image_th);
			}

			if(file_exists($image_wa)){
				unlink($image_wa);
			}

			if(!file_exists($image) && !file_exists($image_th) && !file_exists($image_wa)){
				return true;
			}else{
				return false;
			}


		} 

		function modPic($post){
			if($this->validateForm($post)){
				$image=new Image(GALLERY_REAL_PATH);
				if($_FILES["pic"]["error"]==0){
					//上传图片
					$up=$this->upload();
					if($up[0]){
						global  $pictureSize;
						$imageName=$image->thumb($up[1], $pictureSize["maxWidth"], $pictureSize["maxWidth"],"");
						if($imageName){
							$this->delsrcimg($post["picName"]); //删除原来的图片
							$post["picName"]=$imageName;
							$this->messList[]="图片上传成功";
						}else{
							$this->messList[] = "改变图片大小失败！";
							return false;
						}
					}else{
						$this->messList[]=$up[1];
						return;
					}
				}
				
				if(!empty($post['hasThumb'])){ //生成缩略图
					global $thumbSize;	
					if($image->thumb($post["picName"],$thumbSize["width"], $thumbSize["height"])){
						$this->messList[] = "修改缩略图片成功！";
					}else{
						$this->messList[] = "修改缩略图片失败！";
						return false;
					}
				}else{
					$post['hasThumb']=0;
				}
				if(!empty($post['hasMark'])){	//加水印
					global $waterText;
					if($image->waterMark($post["picName"], WATER, POSITION)){
						$this->messList[] = "修改水印成功！";
					}else{
						$this->messList[] = "修改水印失败！";
						return false;
					}
				}else{
					$post['hasMark']=0;
				}

				//向数据库中添加图片
				if($this->mod($post)){
					$this->messList[] = "图片修改成功！";
					return true;
				}else{
					$this->messList[] = "图片记录修改失败！";
					return false;
				}
			}else{
				$this->messList[] = "图片修改失败！";
				return false;
			}
		}

		function delImgFile($id){
			if(is_array($id))
				$tmp = "IN (" . join(",", $id) . ")";
			else 
				$tmp = "= $id";
			
			$sql = "SELECT picName  from {$this->tabName}  WHERE id " . $tmp ;

			$result=$this->mysqli->query($sql);
			
			while($row=$result->fetch_assoc()){
				$this->delsrcimg($row["picName"]);
			}	
		}

		//删除记录和上传的所有图片
		function delPic($id){
			$this->delImgFile($id);
			if($this->del($id)){
				$this->messList[] = "数据表记录删除成功！";
				return true;
			}else{
				$this->messList[] = "数据表记录删除失败！";
				return false;
			}
	 
		}

		function getPicPath($id){
			$pic=$this->find($id);
	
			if($pic["hasThumb"]==1){
				$picPath="th_".$pic["picName"];
			}else{
				$picPath=$pic["picName"];
			}

			if(file_exists(GALLERY_REAL_PATH.$picPath)){
				return GALLERY_PATH.$picPath;
			}else{
				return false;
			}
		}
	}
